package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.AdministratorMapper;
import com.eco.fanliapp.entity.Administrator;
import com.eco.fanliapp.service.IAdministratorService;
import com.eco.fanliapp.utils.MD5Utils;
import com.eco.fanliapp.utils.MapValueHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;


@Service
public class AdministratorServiceImpl extends ServiceImpl<AdministratorMapper, Administrator> implements IAdministratorService {

    @Autowired
    private AdministratorMapper administratorMapper ;


    @Override
    public Administrator findByAdministratorName(String administratorName) {
        return administratorMapper.findByAdministratorName(administratorName);
    }

    @Override
    public Administrator findByAdministratorNameAndAdministratorPassword(HashMap<String,Object> hashMap) {
        return administratorMapper.findByAdministratorNameAndAdministratorPassword(hashMap);
    }

    @Override
    public ServerResponse insertUserNameAndPwd(HashMap<String, Object> hashMap) {
        MapValueHelper mapValueHelper = new MapValueHelper(hashMap);
        String username = mapValueHelper.getString("username");
        String password = mapValueHelper.getString("password");
        Administrator admin = administratorMapper.findByAdministratorName(username);
        if(admin != null){
            return ServerResponse.createByErrorMessage("该账户名已存在");
        }
        String pwd = MD5Utils.createMD5(password);
        hashMap.put("password",pwd);
        int i = administratorMapper.insertUserNameAndPwd(hashMap);
        if(i >0){
            ServerResponse.createBySuccess("注册成功");
        }else {
        return ServerResponse.createByErrorMessage("注册失败");
        }
        return null;
    }

    @Override
    public int update(HashMap<String, Object> hashMap) {
        return administratorMapper.updatePwd(hashMap);
    }
}
